/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;

import net.groupme.exceptions.UserNotExistingException;
import net.groupme.export.constants.MediaTypes;
import net.groupme.export.rdf.TaggingContext;
import net.groupme.model.Group;
import net.groupme.model.ObjectFactory;
import net.groupme.model.Resource;
import net.groupme.model.User;
import net.groupme.model.dao.DATABASE_CONSTANTS;
import net.groupme.utils.StringUtilities;
import net.groupme.utils.sql.GetTaggingContextQuery;

/**
 * This class provides...
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class Utils {
    
    /** Creates a new instance of Utils */
    private Utils() {}
 
    
   
    
    /**
     *
     * @see getGroupsByResourceId
     * @param DataSource
     * @param URI
     * @return Collection<Group>
     */
    public static Collection<Group> getGroupsByResourceUri(DataSource ds, URI resourceUri) {
    	return ObjectFactory.getResourceByURI(resourceUri).getGroups();
    }  
    
    
    /**
     *
     *
     * @param DataSource
     * @param int
     * @param Collection<Group>
     */
    @SuppressWarnings("unchecked")
    public static Collection<Group> getGroupsByGroupId(DataSource ds, int groupId) {
    	Group group = ObjectFactory.getGroup("" + groupId);
        Collection<Group> tempGroup = new ArrayList<Group>();
        tempGroup.add(group);        
        return tempGroup;
    }    
    
       
    /**    
     * 
     *
     * @param DataSource
     * @param int
     * @return Collection<Resource>
     */
    @SuppressWarnings("unchecked")
    public static Collection<Resource> getResourcesByResourceId(DataSource ds, int resourceId) {
    	Resource res = ObjectFactory.getResource("" + resourceId);
    	Collection<Resource> tempCollection = new ArrayList<Resource>();
    	tempCollection.add(res);
        return tempCollection;

    }
    

    /**    
     * 
     *
     * @param DataSource
     * @param int
     * @return Collection<Resource>
     */
    @SuppressWarnings("unchecked")
    public static Collection<Group> getGroupDataByUserId(DataSource ds, int userId) {
    	User user = null;
		try {
			user = ObjectFactory.getUser(userId);
		} catch (UserNotExistingException e) {
			e.printStackTrace();
			return new ArrayList<Group>();
		} 
        return user.getGroups();
    }    
    
    
    /**
     *
     * @see getResourceByResourceId
     * @param DataSource
     * @param URI
     * @return Collection<Resource>
     */
    public static Collection<Resource> getResourcesByResourceUri(DataSource ds, URI uri) {
        
        Integer id = new Integer(uri.toString().substring(uri.toString().indexOf("#")+1));            
        return Utils.getResourcesByResourceId(ds, id);
    }    
    
    
    /**
     *
     *
     * @param DataSource
     * @param URI
     * @return Collection<TaggingContext>
     */
    @SuppressWarnings("unchecked")    
    public static Collection<TaggingContext> getTaggingContextByTagUri(DataSource ds, URI uri) {

        Integer tagId = new Integer(StringUtilities.getIdFromURI(uri));                   
        
        String query = 
            "SELECT " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_USERID + "," +
            "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_GROUPID + "," +
            "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_RESOURCEID + "," +
            "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_DATE + "," +
            "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_IMPORTANCE + 
            "  FROM " + DATABASE_CONSTANTS.TABLE_USERTAGGROUPRESOURCE +
            " WHERE " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_TAGID + " = " + tagId;

        GetTaggingContextQuery contextQuery = new GetTaggingContextQuery(ds, query);
        Iterator iter = contextQuery.execute().iterator();
        Collection<TaggingContext> tempCollection = new ArrayList<TaggingContext>();
        if(iter.hasNext()){
            tempCollection = (Collection<TaggingContext>) iter.next();
        }
        
        return tempCollection;          
    }
    
    /**
    *
    *
    * @param DataSource
    * @param URI
    * @return Collection<TaggingContext>
    */
   @SuppressWarnings("unchecked")    
   public static Collection<TaggingContext> getTaggingContextByResourceId(DataSource ds, String resourceId) {                
       
       String query = 
           "SELECT " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_USERID + "," +
           "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_GROUPID + "," +
           "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_RESOURCEID + "," +
           "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_DATE + "," +
           "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_IMPORTANCE + "," +
           "       " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_TAGID + 
           "  FROM " + DATABASE_CONSTANTS.TABLE_USERTAGGROUPRESOURCE +
           " WHERE " + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_RESOURCEID + " = " + resourceId;

       GetTaggingContextQuery contextQuery = new GetTaggingContextQuery(ds, query);
       Iterator iter = contextQuery.execute().iterator();
       Collection<TaggingContext> tempCollection = new ArrayList<TaggingContext>();
       if(iter.hasNext()){
           tempCollection = (Collection<TaggingContext>) iter.next();
       }
       
       return tempCollection;          
   }
    
    /**
     *
     *
     *
     */
    public static String[] analyseMediaTypes(String[] mediaTypes) {
        
        String[] allowedMediaTypes = new String[] {};
        List<String> list = new ArrayList<String>();
        
        for(String type : mediaTypes) {
            try {
                MediaTypes.valueOf(type.toLowerCase());
                list.add(type);
            } catch (IllegalArgumentException ex) {}
        }
        
        return list.toArray(allowedMediaTypes);
    }
    
    
    /**
     *
     *
     * @param String
     * @return String
     */
    public static String getUriByStringType(String type) {
        
        try {
            MediaTypes mediaType = MediaTypes.valueOf(type.toLowerCase());
            return mediaType.getBaseUri();
        } catch ( IllegalArgumentException ex ) {}
               
        return "";
    }    
    
}
